package org.jeecg.modules.demo.ipds.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.demo.ipds.entity.*;
import org.jeecg.modules.demo.ipds.entity.dto.ByteBitDto;
import org.jeecg.modules.demo.ipds.entity.dto.ByteBitReuseDto;
import org.jeecg.modules.demo.ipds.entity.dto.InsertIpTreeDto;
import org.jeecg.modules.demo.ipds.mapper.ReuseGroupMapper;
import org.jeecg.modules.demo.ipds.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

/**
 * @Description: 复用组
 * @Author: jeecg-boot
 * @Date:   2024-03-05
 * @Version: V1.0
 */
@Service
public class ReuseGroupServiceImpl extends ServiceImpl<ReuseGroupMapper, ReuseGroup> implements IReuseGroupService {


    @Autowired
    private ITreeByteListService treeByteListService;

    @Autowired
    private  IBitListService bitListService;

    @Autowired
    private IByteListReuseService byteListReuseService;

    @Autowired
    private IBitListReuseService bitListReuseService;

    @Autowired
    private IByteFrameZuService byteFrameZuService;

    @Autowired
    private IByteFrameReuseZuService byteFrameReuseZuService;

    @Autowired
    private ITreeByteListService2 treeByteListService2;

    @Autowired
    private ITreeByteListReuseService iTreeByteListReuseService;


    @Override
    public boolean addReuseGroup(ReuseGroup reuseGroup) {

            reuseGroup.setIsXz(0);
            reuseGroup.setIsPublic(0);

            this.baseMapper.insert(reuseGroup);
            String reuse_id = reuseGroup.getId();

            if (reuseGroup.getIds()==null){
            return true;
            }

            List<TreeByteList> treeByteLists = treeByteListService.listByIds(reuseGroup.getIds());
            for (TreeByteList treeByteList : treeByteLists) {
                String byte_id = treeByteList.getId();




                //字节复用
                ByteListReuse byteListReuse = new ByteListReuse(treeByteList, reuse_id);
                boolean save = byteListReuseService.save(byteListReuse);
                //字节复用后id
                String byte_reuse = byteListReuse.getId();

                //子帧组复用
                QueryWrapper<ByteFrameZu> byteFrameZuQueryWrapper = new QueryWrapper<>();
                byteFrameZuQueryWrapper.eq("byte_id",byte_id);
                List<ByteFrameZu> byteFrameZus = byteFrameZuService.list(byteFrameZuQueryWrapper);
                for (ByteFrameZu frameZus : byteFrameZus) {
                    String frameZusId = frameZus.getId();
                    ByteFrameReuseZu byteFrameReuseZu = new ByteFrameReuseZu(frameZus,byte_reuse, reuse_id);
                    byteFrameReuseZuService.save(byteFrameReuseZu);
                    String id = byteFrameReuseZu.getId();
                    //子帧字节复用
                    QueryWrapper<TreeByteList2> treeByteList2QueryWrapper = new QueryWrapper<>();
                    treeByteList2QueryWrapper.eq("pid",frameZusId);
                    List<TreeByteList2> treeByteList2s = treeByteListService2.list(treeByteList2QueryWrapper);
                    for (TreeByteList2 treeByteList2 : treeByteList2s) {
                        String treeByteList2Id = treeByteList2.getId();
                        TreeByteListReuse treeByteListReuse = new TreeByteListReuse(treeByteList2, reuse_id,id);
                        iTreeByteListReuseService.save(treeByteListReuse);
                        String byteId = treeByteListReuse.getId();
                        //子帧位复用

                        QueryWrapper<BitList> bitListQueryWrapper = new QueryWrapper<>();
                        bitListQueryWrapper.eq("byte_id",treeByteList2Id);
                        List<BitList> bitLists = bitListService.list(bitListQueryWrapper);
                            for (BitList bitList : bitLists) {
                                BitListReuse bitListReuse = new BitListReuse(bitList, byteId, reuse_id);
                                  bitListReuseService.save(bitListReuse);

                            }


                    }



                }

                //字节位复用
                QueryWrapper<BitList> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("byte_id",byte_id);
                List<BitList> bitLists = bitListService.list(queryWrapper);
                if (save) {

                    for (BitList bitList : bitLists) {
                        BitListReuse bitListReuse = new BitListReuse(bitList, byte_reuse, reuse_id);
                       bitListReuseService.save(bitListReuse);

                    }
                }
            }

            return true;
    }

    @Override
    public List<ByteListReuse> queryChildById(String id) {
        QueryWrapper<ByteListReuse> byteListReuseQueryWrapper = new QueryWrapper<>();
        byteListReuseQueryWrapper.eq("reuse_id",id);
        byteListReuseQueryWrapper.orderByAsc("index_sx");


        List<ByteListReuse> list = byteListReuseService.list(byteListReuseQueryWrapper);

        for (ByteListReuse byteList : list) {
            List<List<TreeByteListReuse>>  children = new LinkedList<>();
            QueryWrapper<ByteFrameReuseZu> byteFrameZuQueryWrapper =new QueryWrapper<>();
            byteFrameZuQueryWrapper.eq("byte_id",byteList.getId());
            byteFrameZuQueryWrapper.orderByAsc("index_sx");
            List<ByteFrameReuseZu> byteFrameZus = byteFrameReuseZuService.list(byteFrameZuQueryWrapper);
            
            for (ByteFrameReuseZu frameZus : byteFrameZus) {
                QueryWrapper<TreeByteListReuse>  treeByteList2QueryWrapper=  new QueryWrapper<>();
                treeByteList2QueryWrapper.eq("pid", frameZus.getId());
                treeByteList2QueryWrapper.orderByAsc("index_sx");
                List<TreeByteListReuse> byteChildren = iTreeByteListReuseService.list(treeByteList2QueryWrapper);
                //设置子帧
                for (TreeByteListReuse byteChild : byteChildren) {

                    QueryWrapper<BitListReuse> bitQW = new QueryWrapper<>();
                    bitQW.eq("byte_id", byteChild.getId());
                    bitQW.orderByAsc("index_sx");
                    byteChild.setBitLists(bitListReuseService.list(bitQW));
                }
                children.add(byteChildren);

            }





            byteList.setChildren(children);

            QueryWrapper<BitListReuse> bitQW = new QueryWrapper<>();
            bitQW.eq("byte_id", byteList.getId());
            bitQW.orderByAsc("index_sx");
            byteList.setBitLists(bitListReuseService.list(bitQW));
        }

        return list;
    }

    @Override
    public  List<TreeByteList> insertIpTree(InsertIpTreeDto insertIpTreeDto) {
        String reuseId = insertIpTreeDto.getReuseId();
        Integer indexSx = insertIpTreeDto.getIndexSx();
        String ipTreeId = insertIpTreeDto.getIpTreeId();
        //把这个字段下的数据查出来
        List<ByteListReuse> byteListReuses = queryChildById(reuseId);

        List<TreeByteList> byteListCopyTreeBy = new LinkedList<>();
        for (ByteListReuse byteListReus : byteListReuses) {
            //复用组位
            List<BitListReuse> bitLists = byteListReus.getBitLists();
            List<BitList> bitList = new LinkedList<>();

            for (BitListReuse list : bitLists) {

                BitList bit = new BitList(list,ipTreeId);
                bitList.add(bit);
            }

            //复用组子帧
            List<List<TreeByteListReuse>> children = byteListReus.getChildren();
            List<List<TreeByteList2>> children2 = new LinkedList<>();

            for (List<TreeByteListReuse> child : children) {
                List<TreeByteList2> treeByteList2s = new LinkedList<>();
                for (TreeByteListReuse treeByteListReuse : child) {

                    List<BitListReuse> bitLists1 = treeByteListReuse.getBitLists();
                    List<BitList> bitLists2 = new LinkedList<>();

                    for (BitListReuse bitListReuse : bitLists1) {

                        BitList bitList1 = new BitList(bitListReuse,ipTreeId);
                        bitLists2.add(bitList1);
                    }


                    TreeByteList2 treeByteList2 = new TreeByteList2(treeByteListReuse,ipTreeId,bitLists2);


                    treeByteList2s.add(treeByteList2);
                }
                children2.add(treeByteList2s);
            }


            TreeByteList treeByteList = new TreeByteList(byteListReus,ipTreeId,bitList,children2);


            byteListCopyTreeBy.add(treeByteList);
        }


        TreeByteList treeByteList = new TreeByteList();
        treeByteList.setIpTreeId(ipTreeId);
        List<TreeByteList> treeByteLists =     treeByteListService.listByTreeId(treeByteList);


        if (indexSx!=null&&!indexSx.equals("")) {
            treeByteLists.addAll(indexSx, byteListCopyTreeBy);
        }else {
            treeByteLists.addAll(byteListCopyTreeBy);
        }
        //走保存方法
        ByteBitDto byteBitDto = new ByteBitDto();
        byteBitDto.setIpTreeId(ipTreeId);
        byteBitDto.setTreeByteList(treeByteLists);
        treeByteListService.insterBatch(byteBitDto);


        return treeByteLists;
    }

    @Override
    public void insterBatchJson(ByteBitReuseDto byteBitDto) {

        String reuseId = byteBitDto.getReuseId();

        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("reuse_id",reuseId);
        iTreeByteListReuseService.remove(queryWrapper);
        byteListReuseService.remove(queryWrapper);
        bitListReuseService.remove(queryWrapper);
        byteFrameReuseZuService.remove(queryWrapper);


        List<ByteListReuse> byteListReuses = byteBitDto.getByteListReuses();
        if (byteListReuses!=null&&byteListReuses.size()>0){
            //编辑修改sn
            Integer index_sx = 1;

            for (ByteListReuse byteList : byteListReuses) {
                List<List<TreeByteListReuse>> children = byteList.getChildren();
                if (children != null && children.size() > 0) {

                    byteList.setSn(byteList.getIndex());
                    byteList.setReuseId(reuseId);
                    byteList.setIndexSx(index_sx); //设置顺序
                    byteListReuseService.saveOrUpdate(byteList);
                    Integer zuIndex = 1;
                    List<Integer> lengths = new ArrayList<>();
                    for (List<TreeByteListReuse> child : children) {
                        //新增一个组
                        ByteFrameReuseZu byteFrameZu = new ByteFrameReuseZu();
                        byteFrameZu.setName("子帧"+zuIndex);
                        byteFrameZu.setIndexSx(zuIndex);
                        byteFrameZu.setByteId(byteList.getId());
                        byteFrameZu.setReuseId(reuseId);
                        byteFrameReuseZuService.save(byteFrameZu);
                        zuIndex++;
                        Integer byteIndex2 = 1;
                        Integer sum = 0;
                        for (TreeByteListReuse treeByteList2 : child) {
                            sum+=Integer.parseInt(treeByteList2.getFieldLength());
                            //新增子帧字节
                            treeByteList2.setSn(treeByteList2.getIndex());

                            treeByteList2.setPid(byteFrameZu.getId());
                            treeByteList2.setReuseId(reuseId);
                            treeByteList2.setIndexSx(byteIndex2);
                            iTreeByteListReuseService.save(treeByteList2);

                            List<BitListReuse> bitLists = treeByteList2.getBitLists();
                            Integer index_bit_sx = 1;
                            //位保存
                            if (bitLists!=null&&bitLists.size()>0){
                                for (BitListReuse bitList : bitLists) {
                                    bitList.setReuseId(reuseId);
                                    bitList.setIndexSx(index_bit_sx);
                                    bitList.setByteId(treeByteList2.getId());
                                    index_bit_sx ++ ;

                                    bitListReuseService.saveOrUpdate(bitList);
                                }
                            }
                        }
                        lengths.add(sum);
                    }

                    Integer maxLength = lengths.stream().max(Comparator.comparing(Integer::intValue)).get();
                    byteList.setFieldLength(maxLength.toString());
                    byteListReuseService.saveOrUpdate(byteList);

                }else {

                    byteList.setSn(byteList.getIndex());
                    byteList.setReuseId(reuseId);
                    byteList.setIndexSx(index_sx); //设置顺序
                    byteListReuseService.saveOrUpdate(byteList);
                }

                index_sx ++ ;
                List<BitListReuse> bitLists = byteList.getBitLists();
                Integer index_bit_sx = 1;

                //位保存
                if (bitLists!=null&&bitLists.size()>0){
                    for (BitListReuse bitList : bitLists) {
                        bitList.setReuseId(reuseId);
                        bitList.setIndexSx(index_bit_sx);
                        bitList.setByteId(byteList.getId());
                        index_bit_sx ++ ;

                        bitListReuseService.saveOrUpdate(bitList);
                    }
                }
            }
        }
    }


}
